﻿package mortal.game.view.worship
{
    import Message.Game.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class WorshipController extends Controller
    {
        private var _module:WorshipModule;

        public function WorshipController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._module == null)
            {
                this._module = new WorshipModule();
                this._module.addEventListener(WindowEvent.SHOW, this.onShow);
                this._module.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._module;
        }// end function

        protected function onShow(event:WindowEvent) : void
        {
            Dispatcher.addEventListener(EventName.worshipPlayRequest, this.onPlayRequest);
            Dispatcher.addEventListener(EventName.WorshipPlaySuccess, this.onPlaySuccess);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onLikaiZhucheng);
            return;
        }// end function

        protected function onClose(event:WindowEvent) : void
        {
            Dispatcher.removeEventListener(EventName.worshipPlayRequest, this.onPlayRequest);
            Dispatcher.removeEventListener(EventName.WorshipPlaySuccess, this.onPlaySuccess);
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onLikaiZhucheng);
            return;
        }// end function

        protected function onLikaiZhucheng(event:Event) : void
        {
            if (isViewShow)
            {
                this._module.hide();
            }
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.ClickCityKing, this.onViewShow);
            NetDispatcher.addCmdListener(ServerCommand.WorshipInfo, this.onGetInfo);
            Dispatcher.addEventListener(EventName.WorshipInfoRequest, this.onRequestWorshipInfo);
            return;
        }// end function

        private function onPlaySuccess(event:DataEvent) : void
        {
            var _loc_2:int = 0;
            (Cache.instance.worship.info.adoreTimes + 1);
            (Cache.instance.worship.info.kingBeAdoreTimes + 1);
            if (int(event.data) == 0)
            {
                _loc_2 = ZazenRewardConfig.instance.getInfoById(Cache.instance.role.entityInfo.level).adoreBindCoin;
                MsgManager.showRollTipsMsg(Language.getStringByParam(90464, String(_loc_2)));
            }
            else if (int(event.data) == 1)
            {
                _loc_2 = ZazenRewardConfig.instance.getInfoById(Cache.instance.role.entityInfo.level).adoreUnbindCoin;
                MsgManager.showRollTipsMsg(Language.getStringByParam(90465, String(_loc_2)));
            }
            this._module.refresh();
            return;
        }// end function

        protected function onRequestWorshipInfo(event:Event) : void
        {
            GameProxy.worship.requestWorshipInfo();
            return;
        }// end function

        protected function onGetInfo(param1:SKingAdoreInfo) : void
        {
            Cache.instance.worship.info = param1;
            this.refresh();
            return;
        }// end function

        protected function refresh(param1 = null) : void
        {
            if (isViewShow)
            {
                this._module.refresh();
            }
            return;
        }// end function

        private function onViewShow(event:Event) : void
        {
            if (!isViewShow)
            {
                view.show();
            }
            else
            {
                view.hide();
            }
            return;
        }// end function

        private function onPlayRequest(event:DataEvent) : void
        {
            GameProxy.worship.worshipPlay(int(event.data));
            return;
        }// end function

    }
}
